package exhi.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stp.core.entity.IGridRowFactory;
import com.stp.core.entity.XGrid;
import com.stp.core.entity.XGridPage;
import com.stp.core.entity.XGridRow;
import com.stp.core.util.Identities;
import com.stp.std.entity.AuditLog;

import exhi.entity.EntpBexhi;
import exhi.entity.EntpSexhi;
import exhi.manager.EntpBexhiMng;
import exhi.manager.EntpSexhiMng;

@Controller
@RequestMapping("/gov")
public class GovExhiAct extends ViewExhiAct {

	private static final String PATH_TABLE = "/gov-exhi-table";

	private static final String PATH_LIST = "/exhi-list";
	private static final String PATH_VIEW = "/gov-exhi-view";
	private static final String PATH_CHECK = "/gov-exhi-check";
	private static final String PATH_SAVE = "/exhi-save";

	private static final String PATH_TABLE2 = "/gov-sexhi-table";
	private static final String PATH_LIST2 = "/sexhi-list";
	private static final String PATH_CHECK2 = "/gov-sexhi-check";
	private static final String PATH_VIEW2 = "/gov-sexhi-view";
	private static final String PATH_SAVE2 = "/sexhi-save";

	private String code = "";

	@RequestMapping(PATH_TABLE)
	public void table(
			@RequestParam(value = "code", required = true, defaultValue = "") String code,
			ModelMap model) {
		model.addAttribute("code", code);
		if ("20".equalsIgnoreCase(code)) {
			model.addAttribute("colName", "展览会项目预约");
		}

		if ("30".equalsIgnoreCase(code)) {
			model.addAttribute("colName", "展览会项目纸质申报");
		}

		if ("40".equalsIgnoreCase(code)) {
			model.addAttribute("colName", "展览会项目收取材料");
		}

		if ("50".equalsIgnoreCase(code)) {
			model.addAttribute("colName", "展览会项目审核");
		}

		if ("60".equalsIgnoreCase(code)) {
			model.addAttribute("colName", "展览会项目合同打印");
		}

		if ("90".equalsIgnoreCase(code)) {
			model.addAttribute("colName", "展览会项目查询");
		}
	}

	@RequestMapping(PATH_TABLE2)
	public void table2(
			@RequestParam(value = "code", required = true, defaultValue = "") String code,
			ModelMap model) {
		model.addAttribute("code", code);
		if ("20".equalsIgnoreCase(code)) {
			model.addAttribute("colName", "展览会项目预约");
		}

		if ("30".equalsIgnoreCase(code)) {
			model.addAttribute("colName", "展览会项目纸质申报");
		}

		if ("40".equalsIgnoreCase(code)) {
			model.addAttribute("colName", "展览会项目收取材料");
		}

		if ("50".equalsIgnoreCase(code)) {
			model.addAttribute("colName", "展览会项目审核");
		}

		if ("60".equalsIgnoreCase(code)) {
			model.addAttribute("colName", "展览会项目合同打印");
		}

		if ("90".equalsIgnoreCase(code)) {
			model.addAttribute("colName", "展览会项目查询");
		}
	}

	// 权限json数据
	@RequestMapping(PATH_LIST)
	@ResponseBody
	public String infoList(
			XGridPage page,
			@RequestParam(value = "code", required = true, defaultValue = "") String code,
			@RequestParam("cap_code") String capCode,
			@RequestParam("cap_name") String capName)
			throws InstantiationException, IllegalAccessException {
		this.code = code;
		String sql = "from EntpBexhi info where 1=1 ";
		sql += sqlModel.getSQLChars("info.status", "=", code, false, "and");
		sql += sqlModel
				.getSQLChars("info.zlhmc", "like", capName, false, "and");
		sql += " order by info.createtime desc";
		ExhiGridRow row = new GovExhiAct.ExhiGridRow();
		XGrid grid = entpBexhiMng.getXGrid(sql, row, page.getPageNo(),
				page.getPageSize(), false);
		if (null == grid)
			return "";
		return "{\"xGrid\":" + toJson(grid) + "}";
	}

	// 权限json数据
	@RequestMapping(PATH_LIST2)
	@ResponseBody
	public String infoList2(
			XGridPage page,
			@RequestParam(value = "code", required = true, defaultValue = "") String code,
			@RequestParam("cap_code") String capCode,
			@RequestParam("cap_name") String capName)
			throws InstantiationException, IllegalAccessException {
		this.code = code;
		String sql = "from EntpSexhi info where zhlx='S' ";
		sql += sqlModel.getSQLChars("info.status", "=", code, false, "and");
		sql += sqlModel
				.getSQLChars("info.zlhmc", "like", capName, false, "and");
		sql += " order by info.createtime desc";
		SExhiGridRow row = new GovExhiAct.SExhiGridRow();
		XGrid grid = entpSexhiMng.getXGrid(sql, row, page.getPageNo(),
				page.getPageSize(), false);
		if (null == grid)
			return "";
		return "{\"xGrid\":" + toJson(grid) + "}";
	}

	class SExhiGridRow implements IGridRowFactory {
		public XGridRow builderXRow(Object obj) {
			// TODO Auto-generated method stub
			XGridRow xrow = null;
			if (obj instanceof EntpSexhi) {
				xrow = new XGridRow();
				EntpSexhi info = (EntpSexhi) obj;
				xrow.setId(info.getId());
				List t = new ArrayList();
				t.add("");
				t.add(info.getZlhmc());
				String s = "";
				if ("20".equalsIgnoreCase(code) || "50".equalsIgnoreCase(code)
						|| "40".equalsIgnoreCase(code)) {
					s = "<a href='" + PATH_CHECK2.substring(1)
							+ ".jspx?sinfoId=" + info.getId() + "&code=" + code
							+ "'>【审查】</a><a href='#'>暂停操作</a>";
				} else {
					if ("60".equalsIgnoreCase(code)) {
						s = "<a href='" + PATH_VIEW2.substring(1)
								+ ".jspx?sinfoId=" + info.getId() + "&code="
								+ code + "'>【展览详情】</a><a href='#'>暂停操作</a>【受理】<a href='print-corp-exhi.jspx'>【批复发放】</a> ";
					} else {
						s = "<a href='" + PATH_VIEW2.substring(1)
								+ ".jspx?infoId=" + info.getId() + "&code="
								+ code + "'>【展览详情】</a><a href='#'>暂停操作</a>";
					}
				}
				t.add(s);
				xrow.setData(t);
			}
			return xrow;
		}
	}

	class ExhiGridRow implements IGridRowFactory {
		public XGridRow builderXRow(Object obj) {
			// TODO Auto-generated method stub
			XGridRow xrow = null;
			if (obj instanceof EntpBexhi) {
				xrow = new XGridRow();
				EntpBexhi info = (EntpBexhi) obj;
				xrow.setId(info.getId());
				List t = new ArrayList();
				t.add("");
				t.add(info.getZlhmc());
				t.add(getUrl(info.getId()));
				xrow.setData(t);
			}
			return xrow;
		}
	}

	private String getUrl(String id) {
		String s = "";
		if ("20".equalsIgnoreCase(code) || "50".equalsIgnoreCase(code)
				|| "40".equalsIgnoreCase(code)) {
			s = "<a href='" + PATH_CHECK.substring(1) + ".jspx?infoId=" + id
					+ "&code=" + code + "'>【审查】</a><a href='#'>暂停操作</a>";
		} else {
			if ("60".equalsIgnoreCase(code)) {
				s = "<a href='" + PATH_VIEW.substring(1) + ".jspx?infoId=" + id
						+ "&code=" + code + "'>【展览详情】</a><a href='#'>暂停操作</a>【受理】<a href='print-corp-exhi.jspx'>【批复发放】</a> ";
			} else {
				s = "<a href='" + PATH_VIEW.substring(1) + ".jspx?infoId=" + id
						+ "&code=" + code + "'>【展览详情】</a><a href='#'>暂停操作</a>";
			}
		}
		return s;
	}

	@RequestMapping(PATH_VIEW)
	public void infoView(@RequestParam("code") String code,
			@ModelAttribute("entpBexhi") EntpBexhi entpBexhiInfo, ModelMap model) {
		model.addAttribute("code", code);
		model.addAttribute("info", entpBexhiInfo);
		showBexhiView(entpBexhiInfo, model);
	}

	@RequestMapping(PATH_CHECK)
	public void infoCheck(@RequestParam("code") String code,
			@ModelAttribute("entpBexhi") EntpBexhi entpBexhiInfo, ModelMap model) {
		model.addAttribute("code", code);
		model.addAttribute("info", entpBexhiInfo);
		showBexhiView(entpBexhiInfo, model);
	}

	@RequestMapping(PATH_CHECK2)
	public void infoCheck(@RequestParam("code") String code,
			@ModelAttribute("entpSexhi") EntpSexhi entpSexhiInfo, ModelMap model) {
		model.addAttribute("code", code);
		model.addAttribute("info", entpSexhiInfo);
		showSexhiView(entpSexhiInfo, model);
	}

	// 数据保存
	@RequestMapping(PATH_SAVE)
	public String saveInfo(@RequestParam("code") String code,
			@RequestParam("newStatus") String status,
			@RequestParam("auditName") String auditName,
			@ModelAttribute("entpBexhi") EntpBexhi entpBexhiInfo, ModelMap model)
			throws IllegalStateException {
		if ("21".equalsIgnoreCase(status) || "22".equalsIgnoreCase(status)
				|| "23".equalsIgnoreCase(status)
				|| "24".equalsIgnoreCase(status))
			entpBexhiInfo.setStatus("25");
		else
			entpBexhiInfo.setStatus(status);
		entpBexhiMng.save(entpBexhiInfo);
		AuditLog audit = new AuditLog();
		audit.setAuditGroup("B");
		audit.setAuditId(entpBexhiInfo.getId());
		if ("21".equalsIgnoreCase(status))
			audit.setAuditTitle("档期冲突请调整");
		if ("22".equalsIgnoreCase(status))
			audit.setAuditTitle("特殊题材,请补充材料	");
		if ("23".equalsIgnoreCase(status))
			audit.setAuditTitle("退回调整,填写具体内容");
		if ("30".equalsIgnoreCase(status))
			audit.setAuditTitle("同意");
		if ("45".equalsIgnoreCase(status))
			audit.setAuditTitle("退回调整");
		if ("50".equalsIgnoreCase(status))
			audit.setAuditTitle("信息已核对，已收取纸质材料");
		if ("55".equalsIgnoreCase(status))
			audit.setAuditTitle("退回调整");
		if ("60".equalsIgnoreCase(status))
			audit.setAuditTitle("项目审核通过");
		audit.setAuditSendId(status);
		audit.setAuditTime(new Date());
		audit.setAuditName(auditName);
		audit.setAuditStatus(status);
		auditLogMng.save(audit);
		return "redirect:" + PATH_TABLE.substring(1) + ".jspx?code=" + code;
	}

	// 数据保存
	@RequestMapping(PATH_SAVE2)
	public String saveInfo2(@RequestParam("code") String code,
			@RequestParam("newStatus") String status,
			@RequestParam("auditName") String auditName,
			@ModelAttribute("entpSexhi") EntpSexhi entpSexhiInfo, ModelMap model)
			throws IllegalStateException {
		entpSexhiInfo.setStatus(status);
		entpSexhiMng.save(entpSexhiInfo);
		AuditLog audit = new AuditLog();
		audit.setAuditGroup("S");
		audit.setAuditId(entpSexhiInfo.getId());
		if ("25".equalsIgnoreCase(status))
			audit.setAuditTitle("退回修改");
		if ("30".equalsIgnoreCase(status))
			audit.setAuditTitle("信息已核对，请提交纸质材料");
		if ("45".equalsIgnoreCase(status))
			audit.setAuditTitle("退回调整");
		if ("50".equalsIgnoreCase(status))
			audit.setAuditTitle("信息已核对，已收取纸质材料");
		if ("55".equalsIgnoreCase(status))
			audit.setAuditTitle("退回调整");
		if ("60".equalsIgnoreCase(status))
			audit.setAuditTitle("项目审核通过");
		audit.setAuditSendId(status);
		audit.setAuditTime(new Date());
		audit.setAuditName(auditName);
		audit.setAuditStatus(status);
		auditLogMng.save(audit);
		return "redirect:" + PATH_TABLE2.substring(1) + ".jspx?code=" + code;
	}

	@ModelAttribute("entpBexhi")
	public EntpBexhi getEntpBexhiInfo(
			@RequestParam(value = "infoId", required = true, defaultValue = "") String id) {
		EntpBexhi entpBexhi = entpBexhiMng.get(id);
		if (null == entpBexhi) {
			entpBexhi = new EntpBexhi();
		}
		return entpBexhi;
	}

	@ModelAttribute("entpSexhi")
	public EntpSexhi getEntpSexhiInfo(
			@RequestParam(value = "sinfoId", required = true, defaultValue = "") String id) {
		EntpSexhi entpSexhi = entpSexhiMng.get(id);
		if (null == entpSexhi) {
			entpSexhi = new EntpSexhi();
		}
		return entpSexhi;
	}

	@Autowired
	@Qualifier("entpBexhiMng")
	private EntpBexhiMng entpBexhiMng;

	@Autowired
	@Qualifier("entpSexhiMng")
	private EntpSexhiMng entpSexhiMng;

}
